package com.yxhl.zoume.common.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.presenter.UpPullPresenter;
import com.yxhl.zoume.utils.LOG;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;

/**
 * Created by sugarya on 16/5/23.
 * 包含上拉加载逻辑,RecyclerView适配器
 */
public abstract class UpPullLoadDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "UpPullLoadDataAda";

    protected Context mContext;
    protected LinearLayoutManager mLayoutManager;
    protected List<? extends Serializable> mDataList;
    protected UpPullPresenter mUpPullPresenter;

    private int mFootType;
    /**
     * RecyclerView 上拉加载图标是否可见
     */
    private FootViewHolder mFootViewHolder;
    private int mScrollDy;

    public UpPullLoadDataAdapter(Context mContext,
                                 LinearLayoutManager mLayoutManager,
                                 List<? extends Serializable> dataList,
                                 UpPullPresenter upPullPresenter) {
        this.mContext = mContext;
        this.mLayoutManager = mLayoutManager;
        this.mDataList = dataList;
        this.mUpPullPresenter = upPullPresenter;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < getCustomItemCount()) {
            return getCustomItemViewType(position);
        } else {
            mFootType = getCustomItemViewType(position) + 1;
            return mFootType;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == mFootType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_recycler_passenger_foot, parent, false);
            return new FootViewHolder(view);
        } else {
            return onCustomCreateViewHolder(parent, viewType);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof FootViewHolder) {
            mFootViewHolder = (FootViewHolder) holder;
            mFootViewHolder.onBindFootViewHolder();
        } else {
            onCustomBindViewHolder(holder, position);
        }
    }

    @Override
    public int getItemCount() {
        return getCustomItemCount() + 1;
    }

    /**
     * 判断是否滑到底部
     *
     * @return true 表示滑到底部
     */
    private boolean isCompleteVisibleItem() {
        int visibleItemPosition = mLayoutManager.findLastCompletelyVisibleItemPosition();
        int size = mDataList.size();

        LOG.e(TAG, "visibleItemPosition=" + visibleItemPosition + "  mDataList.size()=" + size);
        if (visibleItemPosition >= size) {
            return true;
        }
        return false;
    }

    public void setFootViewVisibility(boolean isVisible) {
        if (mFootViewHolder != null) {
            mFootViewHolder.setVisible(isVisible);
        }
    }

    public int getCustomItemViewType(int position) {
        return 0;
    }

    public abstract RecyclerView.ViewHolder onCustomCreateViewHolder(ViewGroup parent, int viewType);

    public abstract void onCustomBindViewHolder(RecyclerView.ViewHolder holder, int position);

    public abstract int getCustomItemCount();

    public class OnUpPullLoadDataScrollListener extends RecyclerView.OnScrollListener {

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            switch (newState) {
                case RecyclerView.SCROLL_STATE_DRAGGING:
                    LOG.e(TAG, "DRAGGING");
                    break;
                case RecyclerView.SCROLL_STATE_IDLE:
                    LOG.e(TAG, "IDLE");
                    if (mScrollDy > 0) {
                        if (isCompleteVisibleItem()) {
                            setFootViewVisibility(true);
                        } else {
                            setFootViewVisibility(false);
                        }
                    } else {
                        setFootViewVisibility(false);
                    }
                    break;
                case RecyclerView.SCROLL_STATE_SETTLING:
                    LOG.e(TAG, "SETTLING");
                    break;
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            mScrollDy = dy;
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder {
        public static final int UP_PULL_REFRESH_DELAY = 600;
        @BindView(R.id.rl_up_pull_foot_container)
        RelativeLayout relativeLayout;

        @BindView(R.id.bar_up_pull_foot)
        MaterialProgressBar upPullFootBar;

        public FootViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(FootViewHolder.this, itemView);
        }

        /**
         * 设置初始化显示
         */
        void onBindFootViewHolder() {
            if (isCompleteVisibleItem()) {
                if (mScrollDy > 0) {
                    setVisible(true);
                } else {
                    setVisible(false);
                }
            } else {
                setVisible(false);
            }
        }

        void setVisible(boolean enableVisible) {
            if (enableVisible) {
                relativeLayout.setVisibility(View.VISIBLE);
                relativeLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mUpPullPresenter.loadUpPullData();
                    }
                }, UP_PULL_REFRESH_DELAY);

            } else {
                relativeLayout.setVisibility(View.INVISIBLE);
            }
        }

    }


}
